package com.docking.heylo.service.impl;

import com.docking.heylo.dao.entity.Appointment;
import com.docking.heylo.dao.entity.PayOrder;
import com.docking.heylo.dao.entity.PayOrderExample;
import com.docking.heylo.dao.entity.PersonalInformation;
import com.docking.heylo.dao.mapper.AppointmentMapper;
import com.docking.heylo.dao.mapper.PayOrderMapper;
import com.docking.heylo.dao.mapper.PersonalInformationMapper;
import com.docking.heylo.dao.mapper.UserMapper;
import com.docking.heylo.enums.EAppointmentAppeal;
import com.docking.heylo.enums.EAppointmentState;
import com.docking.heylo.exception.GeneralException;
import com.docking.heylo.service.IAppointmentService;
import com.docking.heylo.web.dto.OrderDto;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class AppointmentServiceImpl implements IAppointmentService {

    @Resource
    private AppointmentMapper appointmentMapper;

    @Resource
    private PersonalInformationMapper personalInformationMapper;

    @Resource
    private PayOrderMapper payOrderMapper;


    /***
     * 订单列表
     *
     * @param orderDto
     * @return
     */
    @Override
    public PageInfo<Map<String, Object>> orderList(OrderDto orderDto) {
        PageHelper.startPage(orderDto.getPageNumber(), orderDto.getPageSize());
        return new PageInfo<>(appointmentMapper.orderList(orderDto.getUserId(), orderDto.getUserPhone(),
                orderDto.getAppeal() == null ? false : orderDto.getAppeal(), orderDto.getAppealStatus(), orderDto.getState()));
    }

    /***
     * 订单详情
     *
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> orderDetail(Integer id) {
        // 订单信息
        Appointment appointment = appointmentMapper.selectByPrimaryKey(id);

        // 摄影师
        Map<String, Object> p = personalInformationMapper.simpleUserInfo(appointment.getPhotographerId());

        // 模特
        Map<String, Object> m = personalInformationMapper.simpleUserInfo(appointment.getUserId());

        // 支付记录
        PayOrderExample example =  new PayOrderExample();
        PayOrderExample.Criteria criteria = example.createCriteria();
        criteria.andAppointmentIdEqualTo(id);
        example.setOrderByClause(" crt_at desc ");
        List<PayOrder> payHis = payOrderMapper.selectByExample(example);

        return new HashMap<String, Object>(){{
            put("appointment", appointment);
            put("photographer", p);
            put("mote", m);
            put("payHis", payHis);
        }};
    }

    @Override
    @Transactional
    public void appealHandling(Appointment appointment) {
        Appointment dbAppointment = appointmentMapper.selectByPrimaryKey(appointment.getId());
        if(dbAppointment.getAppeal().intValue() != EAppointmentAppeal.appealing.getValue()){
            throw new GeneralException("订单不存在或已处理");
        }

        appointmentMapper.updateByPrimaryKeySelective(new Appointment(){{
            setId(appointment.getId());
            setAppealSucc(appointment.getAppealSucc());
            setAppeal(EAppointmentAppeal.processed.getValue());
        }});
    }






}
